package com.qf.electronic.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.electronic.common.R;
import com.qf.electronic.dto.DispatchDto;
import com.qf.electronic.dto.InspectionDto;
import com.qf.electronic.pojo.Inspection;
import com.qf.electronic.mapper.InspectionMapper;
import com.qf.electronic.pojo.User;
import com.qf.electronic.service.IInspectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.electronic.service.IUserService;
import com.qf.electronic.vo.InspectionVo;
import com.qf.sms.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@Service
public class InspectionServiceImpl extends ServiceImpl<InspectionMapper, Inspection> implements IInspectionService {


    public static final ScheduledExecutorService MSG_POOL = Executors.newScheduledThreadPool(2);

    @Autowired
    private SmsService smsService;

    @Autowired
    private IUserService userService;

    @Value("${spring.mail.username}")
    private String mailSendUser;

    @Autowired
    private JavaMailSender sender;

    @Transactional(readOnly = true, isolation = Isolation.READ_COMMITTED)
    @Override
    public Page<InspectionVo> searchInspections(InspectionDto inspectionDto) {
        Page<Inspection> page = new Page<>(inspectionDto.getCurrentPage(), inspectionDto.getPageSize());
        return this.baseMapper.getInspections(page, inspectionDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean dispatchInspection(DispatchDto dispatchDto) {
        UpdateWrapper<Inspection> wrapper = new UpdateWrapper<>();
        wrapper.set("inspection_user", dispatchDto.getOperationUser());
        wrapper.set("state", 1);
        wrapper.eq("id", dispatchDto.getId());
        //分配成功后需要发送短信或者邮件通知巡检员
        if (update(wrapper)) {
            MSG_POOL.schedule( () -> {
                User user = userService.getById(dispatchDto.getOperationUser());
                String phone = user.getPhone();//这个就是发送短信的手机号
                smsService.sendSms(phone, "你有一个新的巡检任务，请在APP中查阅详情");

                String email = user.getEmail(); //这个就是发送邮件的邮箱
                SimpleMailMessage mailMessage = new SimpleMailMessage();
                mailMessage.setFrom(mailSendUser);
                mailMessage.setTo(email);
                mailMessage.setSubject("巡检任务");
                mailMessage.setText("你有一个新的巡检任务，请在APP中查阅详情");
                sender.send(mailMessage);
                //如果需要发送邮件，那么还需要查询分配的用户的邮箱信息
            }, 5, TimeUnit.SECONDS);
            return true;
        }

        return false;
    }
}
